//
// Created by 王乐 on 2022/2/16.
//

#include "LeCubicSubBoxBehaviour.h"

#include "LeInput.h"
#include "LeScene.h"

#include "LeCameraManager.h"
#include "LeCameraCADBehaviour.h"

#include "LeUtils.h"
#include "LeTime.h"

LeCubicSubBoxBehaviour::LeCubicSubBoxBehaviour()
{
}

LeCubicSubBoxBehaviour::~LeCubicSubBoxBehaviour()
{

}

void LeCubicSubBoxBehaviour::Start()
{
    m_Mode = CSBNORM;
    m_CurID = -1;

    m_ChangingMaxTime = 1;
    m_CurChangingTime = 0;
    m_ChangeRatio = 0.3;
    m_pCameraBehaviour = dynamic_cast<LeCameraCADBehaviour*>(LeCameraManager::pCameraObject->m_pBehaviour);
}

void LeCubicSubBoxBehaviour::Update()
{
    switch (m_Mode)
    {
        case CSBNORM:       processNorm();      break;
        case CSBCHANGING:   processChanging();  break;
    }
}

void LeCubicSubBoxBehaviour::processNorm()
{
    if(m_pCameraBehaviour->isChanging)
        return;

    if(LeInput::getMouseButton(GLFW_MOUSE_BUTTON_LEFT) == GLFW_PRESS)
    {
        if(LeScene::getInstance()->getCurMouseHoverID() == m_pGameObject->ID)
        {
            m_CurID = m_pGameObject->ID;
            m_Mode = CSBCHANGING;
            m_pCameraBehaviour->isChanging = true;

            switch (m_CurID)
            {
                case 3:
                    break;
                case 4:
                    if(LeMath::absFloat(0 - m_pCameraBehaviour->yaw) > 180)
                        m_pCameraBehaviour->yaw += 360;
                    break;
                case 5:
                    break;
                case 6:
                    break;
                case 7:
                    break;
                case 8:
                    if(LeMath::absFloat(-270 - m_pCameraBehaviour->yaw) > 180)
                        m_pCameraBehaviour->yaw -= 360;
                    break;
            }
        }
    }
}

void LeCubicSubBoxBehaviour::processChanging()
{
    m_CurChangingTime += LeTime::dDeltaTime;

    switch (m_CurID)
    {
        case 3:
            m_pCameraBehaviour->yaw = LeMath::lerpFloat(m_pCameraBehaviour->yaw, -180, m_ChangeRatio);
            m_pCameraBehaviour->pitch = LeMath::lerpFloat(m_pCameraBehaviour->pitch, 0, m_ChangeRatio);
            break;
        case 4:
            m_pCameraBehaviour->yaw = LeMath::lerpFloat(m_pCameraBehaviour->yaw, 0, m_ChangeRatio);
            m_pCameraBehaviour->pitch = LeMath::lerpFloat(m_pCameraBehaviour->pitch, 0, m_ChangeRatio);
            break;
        case 5:
            m_pCameraBehaviour->yaw = LeMath::lerpFloat(m_pCameraBehaviour->yaw, -270, m_ChangeRatio);
            m_pCameraBehaviour->pitch = LeMath::lerpFloat(m_pCameraBehaviour->pitch, -89, m_ChangeRatio);
            break;
        case 6:
            m_pCameraBehaviour->yaw = LeMath::lerpFloat(m_pCameraBehaviour->yaw, -270, m_ChangeRatio);
            m_pCameraBehaviour->pitch = LeMath::lerpFloat(m_pCameraBehaviour->pitch, 89, m_ChangeRatio);
            break;
        case 7:
            m_pCameraBehaviour->yaw = LeMath::lerpFloat(m_pCameraBehaviour->yaw, -90, m_ChangeRatio);
            m_pCameraBehaviour->pitch = LeMath::lerpFloat(m_pCameraBehaviour->pitch, 0, m_ChangeRatio);
            break;
        case 8:
            m_pCameraBehaviour->yaw = LeMath::lerpFloat(m_pCameraBehaviour->yaw, -270, m_ChangeRatio);
            m_pCameraBehaviour->pitch = LeMath::lerpFloat(m_pCameraBehaviour->pitch, 0, m_ChangeRatio);
            break;
    }
    m_pCameraBehaviour->updateCameraVectors();
    m_pCameraBehaviour->updateCameraPosition();


    if(LeInput::getMouseButton(GLFW_MOUSE_BUTTON_LEFT) == GLFW_RELEASE && m_CurChangingTime > m_ChangingMaxTime)
    {
        m_Mode = CSBNORM;
        m_CurChangingTime = 0;

        m_pCameraBehaviour->isChanging = false;
    }
}
